<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 接收来自父组件的数据
const props = defineProps({
  mainMenuItems: {
    type: Array,
    default: () => [
      { label: '首页' },
      {
        label: '学操服务',
        children: ['功能信息', '人才培养', '特色教学'],
      },
    ],
  },
})

const isMobile = ref(false)
const activeDropdown = ref(-1)
const isMobileMenuOpen = ref(false)

const toggleMobileMenu = () => {
  isMobileMenuOpen.value = !isMobileMenuOpen.value
}

// 桌面端悬停逻辑
const showDropdown = (index) => {
  if (!isMobile.value) activeDropdown.value = index
}

const hideDropdown = () => {
  if (!isMobile.value) activeDropdown.value = -1
}

// 路由跳转
const navigateTo = (path) => {
  router.push(path)
}
</script>

<template>
  <nav class="nav-container">
    <!-- 汉堡菜单 -->
    <div class="hamburger" @click="toggleMobileMenu">
      <span></span>
      <span></span>
      <span></span>
    </div>
    <!-- 主导航 -->
    <ul class="main-nav">
      <div class="nav-list" :class="{ active: isMobileMenuOpen }">
        <li
          v-for="(item, index) in props.mainMenuItems"
          :key="index"
          class="nav-item"
          :class="{ 'has-dropdown': item.children }"
          @mouseenter="showDropdown(index)"
          @mouseleave="hideDropdown(index)"
        >
          <span @click="item.path && navigateTo(item.path)">{{ item.label }}</span>
          <span v-if="item.children" class="dropdown-plus">+</span>
          <ul
            v-if="item.children"
            class="dropdown-menu"
            :style="{ display: activeDropdown === index ? 'block' : 'none' }"
          >
            <li
              v-for="(child, childIndex) in item.children"
              :key="childIndex"
              class="dropdown-item"
              @click="navigateTo(child.path)"
            >
              {{ child }}
            </li>
          </ul>
        </li>
      </div>
    </ul>
  </nav>
</template>

<style scoped>
.nav-container {
  background: linear-gradient(90deg, #1e3c72 0%, #6f9ae4 100%);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 100vw;
  height: 80px !important;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
}
.main-nav {
  display: flex;
  justify-content: center;
  padding: 20px 0;
  margin: 0 auto;
}

.nav-list {
  display: flex;
  list-style: none;
  gap: 15px;
}

.nav-item {
  position: relative;
  font-size: 25px;
  color: #fff;
  cursor: pointer;
  transition: all 0.3s ease;
}

.nav-item:hover {
  /* background: rgba(241, 26, 26, 0.1); */
  color: blue;
  transform: translateY(-2px);
}

.dropdown-plus {
  font-size: 20px;
}

.dropdown-menu {
  display: none;
  position: absolute;
  top: 110%;
  left: 0;
  background: #1983d1;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  border-radius: 4px;
  backdrop-filter: blur(10px);
  animation: fadeIn 0.3s ease;
  padding: 10px;
}

.dropdown-item {
  list-style: none;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  color: #fff;
  transition: all 0.2s ease;
  white-space: nowrap;
  width: 100%;
  box-sizing: border-box;
  margin-top: 10px;
}

.dropdown-item:hover {
  color: blue;
  transform: translateY(-2px);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (min-width: 922px) and (max-width: 1024px) {
  .nav-list {
    gap: 5px;
  }
}

@media (min-width: 768px) and (max-width: 922px) {
  .main-nav {
    overflow-x: auto !important;
    display: block;
    position: relative;
  }

  .nav-list {
    width: max-content !important;
    white-space: nowrap;
  }
  .nav-item {
    position: relative;
  }
  .dropdown-menu {
    position: absolute;
    top: 100%;
    left: 0;
    width: 100%;
    z-index: 1001;
    pointer-events: auto;
  }
}

@media (max-width: 768px) {
  .main-nav {
    overflow-x: auto !important;
    width: 100% !important;
    /* height: 3vw; */
    display: block;
  }

  .nav-list {
    width: max-content !important;
    display: none;
    flex-direction: column;
    position: absolute;
    top: 100%;
    right: 0;
    background: rgba(243, 173, 173, 0.95);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    border-radius: 4px;
    backdrop-filter: blur(10px);
    animation: fadeIn 0.3s ease;
    padding: 0.5vw;
  }
  .dropdown-menu {
    display: none;
    position: absolute;
    top: 0;
    left: -110%;
    background: #1983d1;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    border-radius: 4px;
    backdrop-filter: blur(10px);
    animation: fadeIn 0.3s ease;
    padding: 0.5vw;
  }

  .dropdown-item {
    list-style: none;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    color: #fff;
    transition: all 0.2s ease;
    white-space: nowrap;
    width: 100%;
    box-sizing: border-box;
    margin-top: 1vw;
  }

  .nav-list.active {
    display: flex;
  }

  .hamburger {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    position: absolute;
    top: 0.5vw;
    right: 1vw;
    width: 4vw;
    gap: 0.5vw;
    cursor: pointer;
    z-index: 1001;
  }

  .hamburger span {
    display: block;
    width: 100%;
    height: 0.5vw;
    background: #000;
    transition: all 0.3s ease;
  }

  .hamburger.active span:nth-child(1) {
    transform: rotate(45deg) translate(5px, 5px);
  }

  .hamburger.active span:nth-child(2) {
    opacity: 0;
  }

  .hamburger.active span:nth-child(3) {
    transform: rotate(-45deg) translate(5px, -5px);
  }
}
</style>
